package com.wzy.oa_sys.service.impl.robot;

import com.wzy.oa_sys.mapper.user.UserMapper;
import com.wzy.oa_sys.model.pojo.attendance.OaTimeStandards;
import com.wzy.oa_sys.model.pojo.user.OaUser;
import com.wzy.oa_sys.model.request.robot.RobotReq;
import com.wzy.oa_sys.model.response.attendance.AttendanceResp;
import com.wzy.oa_sys.model.response.attendance.VacationResp;
import com.wzy.oa_sys.model.response.file.FileResp;
import com.wzy.oa_sys.model.response.notice.NoticeResp;
import com.wzy.oa_sys.model.response.schedule.BoardroomResp;
import com.wzy.oa_sys.model.response.schedule.ScheduleResp;
import com.wzy.oa_sys.model.response.schedule.UseBoardroomDataResp;
import com.wzy.oa_sys.model.response.task.TaskResp;
import com.wzy.oa_sys.model.response.user.DeptResp;
import com.wzy.oa_sys.model.response.user.PositionResp;
import com.wzy.oa_sys.model.response.user.UserResp;
import com.wzy.oa_sys.service.ChatAssistant;
import com.wzy.oa_sys.service.attendance.AttendanceService;
import com.wzy.oa_sys.service.attendance.TimeStandardService;
import com.wzy.oa_sys.service.attendance.VacationService;
import com.wzy.oa_sys.service.file.FileService;
import com.wzy.oa_sys.service.notice.NoticeService;
import com.wzy.oa_sys.service.robot.RobotService;
import com.wzy.oa_sys.service.schedule.BoardroomService;
import com.wzy.oa_sys.service.schedule.ScheduleService;
import com.wzy.oa_sys.service.schedule.UseBoardroomService;
import com.wzy.oa_sys.service.task.TaskService;
import com.wzy.oa_sys.service.user.DeptService;
import com.wzy.oa_sys.service.user.PositionService;
import com.wzy.oa_sys.service.user.UserService;
import com.wzy.oa_sys.util.JwtUtil;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.time.LocalDateTime;
import java.util.List;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;

@Service
public class RobotServiceImpl implements RobotService {
    @Autowired
    private DeptService deptService;
    @Autowired
    private PositionService positionService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ScheduleService scheduleService;
    @Autowired
    private UseBoardroomService useBoardroomService;
    @Autowired
    private BoardroomService boardroomService;
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private VacationService vacationService;
    @Autowired
    private AttendanceService attendanceService;
    @Autowired
    private TimeStandardService timeStandardService;
    @Autowired
    private FileService fileService;

    @Resource
    InMemoryEmbeddingStore<TextSegment> embeddingStore;

    @Resource
    ChatAssistant chatAssistant;

    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private UserService userService;

    @Override
    public String getRobotReply(RobotReq req, HttpServletRequest request) {
        LocalDateTime now = LocalDateTime.now();
        req.setInput(req.getInput()+",现在时间是"+now.toString());
        String role = jwtUtil.extractUserRole(request);
        Long userId = jwtUtil.extractUserId(request);
        String message = "";
        switch (req.getOption()){
            case "部门管理":
                Document deptDocument = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/部门信息.docx");
                EmbeddingStoreIngestor.ingest(deptDocument, embeddingStore);
                //额度不足
                String chat = chatAssistant.chat(req.getInput()+"，简单的回答");
                System.out.println(chat);
                return chat;
            case "职位管理":
                Document deptDocument1 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/职位信息.docx");
                EmbeddingStoreIngestor.ingest(deptDocument1, embeddingStore);
                //额度不足
                String chat1 = chatAssistant.chat(req.getInput()+"，简单的回答");
                System.out.println(chat1);
                return chat1;
            case "人员管理":
                Document deptDocument2 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/人员信息.docx");
                EmbeddingStoreIngestor.ingest(deptDocument2, embeddingStore);
                //额度不足
                String chat2 = chatAssistant.chat(req.getInput()+"，简单的回答");
                System.out.println(chat2);
                return chat2;
            case "任务管理":
                Document deptDocument3 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/任务信息.docx");
                EmbeddingStoreIngestor.ingest(deptDocument3, embeddingStore);
                //额度不足
                String chat3 = chatAssistant.chat(req.getInput()+"，简单的回答");
                System.out.println(chat3);
                return chat3;
            case "我的任务":
                Document deptDocument4 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/我的任务.docx");
                EmbeddingStoreIngestor.ingest(deptDocument4, embeddingStore);
                //额度不足
                String chat4 = chatAssistant.chat(req.getInput()+"，简单的回答");
                System.out.println(chat4);
                return chat4;
            case "日程管理":
                Document deptDocument5 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/日程信息.docx");
                EmbeddingStoreIngestor.ingest(deptDocument5, embeddingStore);
                //额度不足
                String chat5 = chatAssistant.chat(req.getInput()+"，简单的回答");
                System.out.println(chat5);
                return chat5;
            case "我的日程":
                Document deptDocument6 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/我的日程.docx");
                EmbeddingStoreIngestor.ingest(deptDocument6, embeddingStore);
                //额度不足
                String chat6 = chatAssistant.chat(req.getInput()+"，简单的回答");
                System.out.println(chat6);
                return chat6;
            case "会议室管理":
                Document deptDocument7 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/会议室以及会议室使用情况.docx");
                EmbeddingStoreIngestor.ingest(deptDocument7, embeddingStore);
                //额度不足
                String chat7 = chatAssistant.chat(req.getInput()+"，简单的回答");
                System.out.println(chat7);
                return chat7;
            case "通知管理":
                Document deptDocument8 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/通知信息.docx");
                EmbeddingStoreIngestor.ingest(deptDocument8, embeddingStore);
                //额度不足
                String chat8 = chatAssistant.chat(req.getInput()+"，简单的回答");
                System.out.println(chat8);
                return chat8;
            case "通知列表":
                Document deptDocument9 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/通知列表(我可接收的通知).docx");
                EmbeddingStoreIngestor.ingest(deptDocument9, embeddingStore);
                //额度不足
                String chat9 = chatAssistant.chat(req.getInput()+"，简单的回答");
                System.out.println(chat9);
                return chat9;
            case "请假管理":
                Document deptDocument10 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/请假管理（未审核）.docx");
                EmbeddingStoreIngestor.ingest(deptDocument10, embeddingStore);
                //额度不足
                String chat10 = chatAssistant.chat(req.getInput()+"，简单的回答");
                System.out.println(chat10);
                return chat10;
            case "请假列表":
                Document deptDocument11 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/请假管理（已审核）.docx");
                EmbeddingStoreIngestor.ingest(deptDocument11, embeddingStore);
                //额度不足
                String chat11 = chatAssistant.chat(req.getInput()+"，简单的回答");
                System.out.println(chat11);
                return chat11;
            case "报表列表":
                if(role.contains("SUPERMAN")||role.contains("MANAGER")){
                    Document deptDocument12 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/报表列表.docx");
                    EmbeddingStoreIngestor.ingest(deptDocument12, embeddingStore);
                    //额度不足
                    String chat12 = chatAssistant.chat(req.getInput()+"，简单的回答");
                    System.out.println(chat12);
                    return chat12;
                }else{
                    return "没有权限，无法回答报表列表的相关问题";
                }
            case "审批管理":
                if(role.contains("SUPERMAN")||role.contains("MANAGER")){
                    Document deptDocument12 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/公告审批与请假审批.docx");
                    EmbeddingStoreIngestor.ingest(deptDocument12, embeddingStore);
                    //额度不足
                    String chat12 = chatAssistant.chat(req.getInput()+"，简单的回答");
                    System.out.println(chat12);
                    return chat12;
                }else{
                    return "没有权限，无法回答审批管理的相关问题";
                }
            case "考勤管理":
                Document deptDocument13 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/考勤记录与考勤标准.docx");
                EmbeddingStoreIngestor.ingest(deptDocument13, embeddingStore);
                //额度不足
                String chat13 = chatAssistant.chat(req.getInput()+"，简单的回答");
                System.out.println(chat13);
                return chat13;
            case "文件管理":
                Document deptDocument14 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/文件管理.docx");
                EmbeddingStoreIngestor.ingest(deptDocument14, embeddingStore);
                //额度不足
                String chat14 = chatAssistant.chat(req.getInput()+"，简单的回答");
                System.out.println(chat14);
                return chat14;
            default:
                Document deptDocument15 = FileSystemDocumentLoader.loadDocument("src/main/resources/uploads/整个系统的信息.docx");
                EmbeddingStoreIngestor.ingest(deptDocument15, embeddingStore);
                String s = req.getInput()+"；我在人员信息表中ID为"+userId+"，简单的回答。如果没有合适的答案，请回复“请选择模块以获得更准确的答案”";
                System.out.println(s);
                String chat15 = chatAssistant.chat(s);
                System.out.println(chat15);
                return chat15;
        }
    }
}
